package org.example.sqldemo;

import static android.provider.BaseColumns._ID;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SQLDemo extends Activity implements LocationListener{
	
  private String latitude;
  private String longitude;
  private String altitude;
  private EventDataSQLHelper eventsData;
  private TextView output;
  private TextView gpsdata; 
  private Button delete;
  private LocationManager locMgrGPS = null;
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    output = (TextView) findViewById(R.id.output);
    output.setMovementMethod(new ScrollingMovementMethod());
    
    
    gpsdata = (TextView) findViewById(R.id.gpsdata);
    gpsdata.setText("");
    gpsdata.setMovementMethod(new ScrollingMovementMethod());
    
    delete = (Button)findViewById(R.id.Delete);
    delete.setOnClickListener(mdeleteListener);

    
    locMgrGPS = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    // attr 2 = every x ms look for an update
    // attr 3 = every x meters position changing update
    locMgrGPS.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this);
    
    eventsData = new EventDataSQLHelper(this);
   // addEvent("First GPS Location");
    Cursor cursor = getEvents();
    showEvents(cursor);
  }
  
  @Override
  public void onDestroy() {	  
	  eventsData.close();
	  super.onDestroy();
  }

  private void addEvent(String title) {
    SQLiteDatabase db = eventsData.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(EventDataSQLHelper.LATITUDE, this.latitude);
    values.put(EventDataSQLHelper.LONGITUDE, this.longitude);
    values.put(EventDataSQLHelper.ALTITUDE, this.altitude);
    db.insert(EventDataSQLHelper.TABLE, null, values);

  }

  private Cursor getEvents() {
    SQLiteDatabase db = eventsData.getReadableDatabase();
    Cursor cursor = db.query(EventDataSQLHelper.TABLE, null, null, null, null,
        null, null);
    
    startManagingCursor(cursor);
    return cursor;
  }

  private void showEvents(Cursor cursor) {
    StringBuilder ret = new StringBuilder("");
    while (cursor.moveToNext()) {
      long id = cursor.getLong(0);
      String latitude2 = cursor.getString(1);
      String longitude2 = cursor.getString(2);
      String altitude2 = cursor.getString(3);
      ret.append(id + "\n \t latitude " + latitude2 + "\n \t longitude :" + longitude2 + "\n \t altitude :" + altitude2 +"\n");
    }
    output.setText(ret);
  }

@Override
public void onLocationChanged(Location location) {
	// TODO Auto-generated method stub
    this.latitude = String.valueOf(location.getLatitude());
    this.longitude = String.valueOf(location.getLongitude());
    this.altitude = String.valueOf(location.getAltitude());
    addEvent("Update");
   
    
    this.gpsdata.append("\n\tLatitude: "+location.getLatitude() +"\n\tLongitude: " + location.getLongitude()+"\n\tAltitude: " + location.getAltitude()+"\n");
    Cursor cursor = getEvents();
    showEvents(cursor);
}

private OnClickListener mdeleteListener = new OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    	SQLiteDatabase db = eventsData.getWritableDatabase();
    	db.delete(EventDataSQLHelper.TABLE, null, null);
    	output.setText("");
    	Cursor cursor = getEvents();
        showEvents(cursor);
    }
};


@Override
public void onProviderDisabled(String arg0) {
	// TODO Auto-generated method stub
	
}

@Override
public void onProviderEnabled(String arg0) {
	// TODO Auto-generated method stub
	
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
	// TODO Auto-generated method stub
	
}
}